<?php


namespace app\api\controller\finance;


use app\api\controller\Api;
use app\common\order\model\OrderPay;
use util\Format;

class Payment extends Api
{
    public function index()
    {
        $post = $this->request->post();

        $where = [];
        // 审核状态
        if (!empty($post['check_status'])) $where[] = ['OrderPay.check_status', 'in', $post['check_status']];
        // 付款性质
        if (!empty($post['pay_type'])) $where[] = ['OrderPay.pay_type_id', 'in', $post['pay_type']];
        // total检索
        if (!empty($post['total'])) $where[] = ['total', '=', $post['total']];
        // 付款区间检索
        if (!empty($post['pay_range'])) {
            $payRange = Format::dateRange($post['pay_range']);
            $where[] = ['apply_date', 'between', $payRange];
        }

        $hasWhere = [];
        $hasWhere[] = ['Order.refund_status', '=', 0];
        // $hasWhere[] = ['Order.order_type', '<>', 2];
        if (!empty($post['user_id'])) $hasWhere[] = ['Order.user_id', 'in', $post['user_id']];
        if (!empty($post['company_id'])) $hasWhere[] = ['Order.company_id', 'in', $post['company_id']];
        if (!empty($post['store_id'])) $hasWhere[] = ['Order.store_id', 'in', $post['store_id']];
        if (!empty($post['category_id'])) $hasWhere[] = ['Order.category_id', 'in', $post['category_id']];
        if (!empty($post['order_type'])) $hasWhere[] = ['Order.order_type', 'in', $post['order_type']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $hasWhere[] = ['Order.sign_date', 'between', $signDateRange];
        }
        if (!empty($post['Order.event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $hasWhere[] = ['Order.event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $hasWhere[] = ['Order.contact_no|Order.bridegroom|Order.bridegroom_mobile|Order.bride|Order.bride_mobile', 'like', "%{$post['keyword']}%"];

        if (empty($post['is_export'])) {
            list($cfg) = Format::pagination($post);
            $model = OrderPay::hasWhere('order', $hasWhere);
            $pg = $model->with('order')->where($where)->order('id desc')->paginate($cfg);
            $rows = $pg->getCollection();
            foreach ($rows as $row) {
                $row->append(['type', 'user', 'check_tag']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'order_type_text', 'check_tag']);
            }

            $result = [];
            $result['list'] = $rows;
            $result['pagination'] = Format::pgResult($pg);

            return success($result);
        } else {
            $expData = [];
            $header = [
                'check_status' => '审核状态',
                'sign_company' => '签单公司',
                'cooperation' => '合作模式',
                'order_type' => '订单性质',
                'category' => '服务类型',
                'sales' => '签单销售',
                'bridegroom' => '新郎姓名',
                'bridegroom_mobile' => '新郎电话',
                'bride' => '新娘姓名',
                'bride_mobile' => '新娘电话',
                'event_date' => '举办日期',
                'sign_date' => '签约日期',
                'store' => '酒店名称',
                'pay_type' => '付款性质',
                'pay_total' => '付款金额',
                'apply_date' => '期望付款日期',
                'pay_real_date' => '实际付款日期',
            ];
            $model = OrderPay::hasWhere('order', $hasWhere);
            $rows = $model->with('order')->where($where)->order('id desc')->select();
            foreach ($rows as $row) {
                $row->append(['type', 'user', 'check_tag']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'order_type_text', 'check_tag']);
                $expData[] = [
                    'check_status' => $row->check_tag,
                    'sign_company' => $row->order->company['title'],
                    'cooperation' => $row->order->cooperation['title'],
                    'order_type' => $row->order->order_type_text,
                    'category' => $row->order->category['title'],
                    'sales' => $row->order->user['realname'],
                    'bridegroom' => $row->order->bridegroom,
                    'bridegroom_mobile' => $row->order->bridegroom_mobile,
                    'bride' => $row->order->bride,
                    'bride_mobile' => $row->order->bride_mobile,
                    'event_date' => $row->order->event_date,
                    'sign_date' => $row->order->sign_date,
                    'store' => $row->order->store['title'],
                    'pay_type' => $row->type['title'],
                    'pay_total' => $row->total,
                    'apply_date' => $row->apply_date,
                    'pay_real_date' => $row->pay_real_date,
                ];
            }

            $fileName = '【付款单】' . date('YmdHis');
            $url = \app\common\third\Excel::exportExcel($expData, $header, $fileName);
            return success(['url' => $url], '导出成功');
        }
    }
}
